clear all
set more off
cap log close

****************************************************************************
* This do-file extract data from the Read-only LAD files
*	- age, sex, family type and business income
* 
* Creates the following Stata files:
*	- demographic_min_yyyy.dta.dta
* 	- demographic_min_8299.dta (appended files)
*   - demographic_min_0016.dta (appended files)
* 
****************************************************************************

do "H:\Lavecchia_7086\to-transfer-jan-2022\RESTAT_Replication_Programs\0_Set_Directories.do"
cap log using "$dir_log\1b_Extract_MinDemographics.log", replace

* Step 1: load the data from LAD read-only files

forvalues year=1982(1)2016{
use lin__i age__i`year' fcmp_i`year' sxco_i`year' bgrs_i`year' using "G:\LAD\LAD_DAL_Allcycle_v3\data\data\Stata\lad`year'.dta"
sort lin__i
gen year=`year'
foreach var in age__i fcmp_i sxco_i bgrs_i{
rename `var'`year' `var'
}
save "$dir_data\Capital income years\demographic_min_`year'.dta", replace
}



* Step 2: append into one file (rename the variables to drop the year portion of the name)

**************** 1982 -- 1999 ****************
clear
use "$dir_data\Capital income years\demographic_min_1982.dta"
forvalues year=1983/1999{
append using "$dir_data\Capital income years\demographic_min_`year'.dta"
compress
}
*
save "$dir_data\demographic_min_8299.dta", replace


**************** 2000 -- 2016 ****************
clear
use "$dir_data\Capital income years\demographic_min_2000.dta"
forvalues year=2001/2016{
append using "$dir_data\Capital income years\demographic_min_`year'.dta"
compress
}
*
save "$dir_data\demographic_min_0016.dta", replace
